package com.xindaoapp.frameworkfragment;

import java.util.ArrayList;
import java.util.List;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.PagerTitleStrip;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.xindaoapp.framework.R;
import com.xindaoapp.framework.activity.LoginActivity;
import com.xindaoapp.framework.activity.SettingActivity;

/**
 * ============================================================
 * 
 * 版权 : 新道APP (c) 2014
 * 
 * 作者 : 贺奕凯
 * 
 * 版本 : 1.0
 * 
 * 创建日期 : 2014-6-19 下午3:14:57
 * 
 * 描述 : 个人中心
 * 
 * 修订历史 ：
 * 
 * ============================================================
 **/
@SuppressLint("ValidFragment")
public class CenterFragment extends Fragment implements OnClickListener {
	@ViewInject(R.id.tv_guanzuh)
	private TextView tv_guanzuh;
	@ViewInject(R.id.tv_xihuan)
	private TextView tv_xihuan;
	@ViewInject(R.id.tv_shangjia)
	private TextView tv_shangjia;
	@ViewInject(R.id.vs_me)
	private LinearLayout vs_me;
	@ViewInject(R.id.ll_slide)
	private LinearLayout ll_slide;

	public Context ct;
	public WindowManager wm;
	private View view;
	private ImageView iv_right_jiantou;
	private ImageView mine_setting;
	private ImageView cancel;
	private RelativeLayout userinfo;
	private ViewPager mViewPager;
	private List<View> ViewList;
	private PagerTitleStrip pagerTitleStrip; // viewpager的标题
	private PagerTabStrip pagerTabStrip; // 一个viewpager的指示器，效果就是一个横的粗的下划线
	private List<String> titleList;// viewpager的标题
	private ImageView imageView;// 动画图片
	private int offset = 0;// 动画图片偏移量
	private int currIndex = 0;// 当前页卡编号
	private int bmpW;// 动画图片宽度
	// 1.定义手势识别器
	private GestureDetector detector;

	private TextView textView1, textView2, textView3;
	private RelativeLayout user_tiezi, user_guanzhu, user_fans;
	private Button bt_test_hide;

	// private boolean flag = true; // 是否继续上滑
	// private boolean t_flag = true; // 是否继续上滑
	// 高度相关
	private int screemH;
	private int me_height;
	private int slide_height;
	private int move_height;


	
	public CenterFragment(Context ct) {
		this.ct = ct;
	}
	public CenterFragment(Context ct, WindowManager wm) {
		this.ct = ct;
		this.wm = wm;
	}
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment5, null);
		ViewUtils.inject(this, view); // 加载注释控件
		initView(); // 初始化控件
		InitImageView();
		init();
		initTextView();
		setListener(); // 初始化事件监听

		setDetector(); //初始化滑动模块
		return view;
	}


	private void initTextView() {
		tv_guanzuh.setOnClickListener(new MyOnClickListener(0));
		tv_xihuan.setOnClickListener(new MyOnClickListener(1));
		tv_shangjia.setOnClickListener(new MyOnClickListener(2));
	}

	/**
	 * 2 * 初始化动画
	 */
	private void InitImageView() {
		bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.a_line)
				.getWidth();// 获取图片宽度
		DisplayMetrics dm = new DisplayMetrics();
		wm.getDefaultDisplay().getMetrics(dm);
		int screenW = dm.widthPixels;// 获取分辨率宽度
		screemH = dm.heightPixels; // 获取分辨率高度

		int w = View.MeasureSpec.makeMeasureSpec(0,
				View.MeasureSpec.UNSPECIFIED);
		int h = View.MeasureSpec.makeMeasureSpec(0,
				View.MeasureSpec.UNSPECIFIED);
		vs_me.measure(w, h);
		me_height = vs_me.getMeasuredHeight();
		ll_slide.measure(w, h);
		slide_height = ll_slide.getMeasuredHeight();
		move_height = me_height - slide_height;

		offset = (screenW / 3 - bmpW) / 2;// 计算偏移量
		System.out.println("屏幕高度:" + screenW + "滑动高度:" + move_height);
		Matrix matrix = new Matrix();
		matrix.postTranslate(offset, 0);
		imageView.setImageMatrix(matrix);// 设置动画初始位置
	}

	/**
	 * 
	 * 头标点击监听 3
	 */
	private class MyOnClickListener implements OnClickListener {
		private int index = 0;

		public MyOnClickListener(int i) {
			index = i;
		}

		public void onClick(View v) {
			mViewPager.setCurrentItem(index);
		}

	}

	private void init() {

		View view1 = View.inflate(ct, R.layout.mine_first, null);
		View view2 = View.inflate(ct, R.layout.mine_second, null);
		View view3 = View.inflate(ct, R.layout.mine_third, null);
		ViewList.add(view1);
		ViewList.add(view2);
		ViewList.add(view3);
		// 把适配器和viewpager控件关联起来.
		mViewPager.setAdapter(new MyPagerAdapter());
		mViewPager.setCurrentItem(0);
		mViewPager.setOnPageChangeListener(new MyOnPageChangeListener());
		mViewPager.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				detector.onTouchEvent(event);
				return false;
			}
		});
	}

	private void initView() {
		iv_right_jiantou = (ImageView) view.findViewById(R.id.iv_right_jiantou);
		mine_setting = (ImageView) view.findViewById(R.id.mine_setting);
		userinfo = (RelativeLayout) view.findViewById(R.id.userinfo);
		ViewList = new ArrayList<View>();
		mViewPager = (ViewPager) view.findViewById(R.id.viewpager);

		user_tiezi = (RelativeLayout) view.findViewById(R.id.user_tiezi);
		user_guanzhu = (RelativeLayout) view.findViewById(R.id.user_guanzhu);
		user_fans = (RelativeLayout) view.findViewById(R.id.user_fans);

		imageView = (ImageView) view.findViewById(R.id.cursor);
	}

	@Override
	public void setMenuVisibility(boolean menuVisible) {
		// TODO Auto-generated method stub
		super.setMenuVisibility(menuVisible);
		if (this.getView() != null)
			this.getView()
					.setVisibility(menuVisible ? View.VISIBLE : View.GONE);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.iv_right_jiantou:
			Intent intent1 = new Intent(ct, LoginActivity.class);
			startActivity(intent1);
			break;
		case R.id.mine_setting:
			Intent intent2 = new Intent(ct, SettingActivity.class);
			startActivity(intent2);
			// 播放动画
			getActivity().overridePendingTransition(R.anim.tran_in,
					R.anim.tran_out);
			break;
		case R.id.userinfo:
			Intent intent3 = new Intent(ct, LoginActivity.class);
			startActivity(intent3);
			break;
		default:
			break;
		}

	}

	class MyPagerAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			return ViewList.size();
		}

		/**
		 * 返回true代表复用view对象 官方建议写法 return view == obj;
		 */
		@Override
		public boolean isViewFromObject(View view, Object obj) {
			return view == obj;
		}

		/**
		 * 销毁多余的item position就是将要被销毁的item的索引
		 */
		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			// super.destroyItem(container, position, object); // 父类要求必须实现此方法.
			// 不实现会抛异常.
			container.removeView(ViewList.get(position));
		}

		/**
		 * 当需要加载item时出发此方法 position 就是将要被加载的item的索引
		 */
		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			container.addView(ViewList.get(position), 0);
			return ViewList.get(position);
		}
	}

	/**
	 * 事件监听
	 */
	public void setListener() {
		iv_right_jiantou.setOnClickListener(this);
		mine_setting.setOnClickListener(this);
		userinfo.setOnClickListener(this);
		// bt_test_hide.setOnClickListener(this);
	}

	public class MyOnPageChangeListener implements OnPageChangeListener {

		int one = offset * 2 + bmpW;// 页卡1 -> 页卡2 偏移量
		int two = one * 2;// 页卡1 -> 页卡3 偏移量

		public void onPageScrollStateChanged(int arg0) {

		}

		public void onPageScrolled(int arg0, float arg1, int arg2) {

		}

		public void onPageSelected(int arg0) {
			// Animation animation = new TranslateAnimation(one * currIndex, one
			// * arg0, 0, 0);
			// currIndex = arg0;
			// animation.setFillAfter(true);// True:图片停在动画结束位置
			// animation.setDuration(250);
			// imageView.startAnimation(animation);
			int[] location = new int[2];
			ll_slide.getLocationOnScreen(location);
			int y = location[1];
			switch (arg0) {
			case 0:
				tv_guanzuh.setTextColor(Color.WHITE);
				tv_xihuan.setTextColor(Color.rgb(102, 102, 102));
				tv_shangjia.setTextColor(Color.rgb(102, 102, 102));

				tv_guanzuh.setBackgroundResource(R.drawable.tabmenu_active);
				tv_xihuan.setBackgroundColor(Color.TRANSPARENT);
				tv_shangjia.setBackgroundColor(Color.TRANSPARENT);
				break;
			case 1:
				tv_guanzuh.setTextColor(Color.rgb(102, 102, 102));
				tv_xihuan.setTextColor(Color.WHITE);
				tv_shangjia.setTextColor(Color.rgb(102, 102, 102));

				tv_guanzuh.setBackgroundColor(Color.TRANSPARENT);
				tv_xihuan.setBackgroundResource(R.drawable.tabmenu_active);
				tv_shangjia.setBackgroundColor(Color.TRANSPARENT);

				if (y > 10) {
					// 执行动画
					animation(move_height);
				}
				break;
			case 2:
				tv_guanzuh.setTextColor(Color.rgb(102, 102, 102));
				tv_xihuan.setTextColor(Color.rgb(102, 102, 102));
				tv_shangjia.setTextColor(Color.WHITE);

				tv_guanzuh.setBackgroundColor(Color.TRANSPARENT);
				tv_xihuan.setBackgroundColor(Color.TRANSPARENT);
				tv_shangjia.setBackgroundResource(R.drawable.tabmenu_active);
				
				if (y > 10) {
					// 执行动画
					animation(move_height);
				}
				break;
			default:
				break;
			}
		}
	}

	public void animation(final int height) {
		// 执行动画
		Animation am = new TranslateAnimation(0, 0, 0, -height);
		am.setFillAfter(true);
		am.setDuration(3000);
		vs_me.startAnimation(am);
		// 监听动画事件
		am.setAnimationListener(new Animation.AnimationListener() {
			@Override
			public void onAnimationEnd(Animation arg0) {
				int left = vs_me.getLeft();
				int top = vs_me.getTop() + (int) (-height);
				int width = vs_me.getWidth();
				int height = vs_me.getHeight();
				LinearLayout.LayoutParams params = (android.widget.LinearLayout.LayoutParams) vs_me
						.getLayoutParams();
				params.setMargins(left, top, 0, 0);// 具体数值看你往哪边移动，移动多少。
				vs_me.clearAnimation(); // 清除动画
				vs_me.setLayoutParams(params);
			}

			@Override
			public void onAnimationRepeat(Animation animation) {

			}

			@Override
			public void onAnimationStart(Animation animation) {

			}
		});
	}
	
	@SuppressWarnings("deprecation")
	private void setDetector() {
		detector = new GestureDetector(new SimpleOnGestureListener() {

			public boolean onFling(MotionEvent e1, MotionEvent e2,
					float velocityX, float velocityY) {

				int[] location = new int[2];
				ll_slide.getLocationOnScreen(location);
				int y = location[1];
				//判断上下滑动
				if ((e2.getRawY() - e1.getRawY()) > 165 && y < 10) {
					// 执行动画
					animation(-move_height);
					return true;
				}
				//待调整，适配
				if ((e1.getRawY() - e2.getRawY()) > 165 && y > 30) {
					// 执行动画
					animation(move_height);
					return true;
				}
				return super.onFling(e1, e2, velocityX, velocityY);
			}

		});
	}
}
